/**
 * @(#) CurrentUserObj.java 
 */

package com.springcloud.sc.ucpv5.client.module.datamanager;

import java.util.ArrayList;

import com.springcloud.sc.ucpv5.client.application.message.MessageService;
import com.springcloud.sc.ucpv5.client.commondata.MyNode;
import com.springcloud.sc.ucpv5.client.commondata.MyUser;
import com.springcloud.sc.ucpv5.client.commondata.data.User;
import com.springcloud.sc.ucpv5.client.contentmain.MainConstant;
import com.springcloud.sc.ucpv5.client.module.setting.SettingConstant;
import com.springcloud.sc.ucpv5.client.module.setting.SettingDataManager;
import com.springcloud.sc.ucpv5.client.module.setting.SettingManager;
import com.springcloud.sc.ucpv5.client.util.PublicUtil;

/**
 * Comments : 6-19
 * 
 */

public class LoginDataManager {

	private static LoginDataManager instance;

	public String fileName;

	public static LoginDataManager getInstance() {
		if (instance == null) {
			instance = new LoginDataManager();
		}
		return instance;
	};

	private String name = ""; // 当前登录用户名称
	private String passWord = ""; // 当前登录用户密码
	private String telNum; // 当前登录用户号码

	private MyUser user; // 当前登录用户的用户对象

	/**
	 * 暂时初始用户属性
	 *
	 */
	public void initDcsUser() {
		if (user != null) {
			return;
		}

		if (telNum != null) {
			user = BaseDataManager.getInstance().userGet(this.telNum);
			if (user == null) {
				User user_idl = new User();
				user_idl.UserTelno = telNum;
				user_idl.UserName = name;
				user_idl.Priority = 7;

				user = new MyUser(user_idl, MessageService.getInstance().getPhysicsNodeNumber(), MessageService.getInstance().getLogicNodeNumber(), 0);

				BaseDataManager.getInstance().getLoginNode().getListModel().addUser(user);
			}
		}

		else if (this.name != null) {
			MyUser user = BaseDataManager.getInstance().userGetByUserName(this.getName());
			if (user != null) {
				this.telNum = user.getIdlData().UserTelno;

				this.user = user;
			} else if (this.getName().equals("admin")) {
				this.telNum = MainConstant.AdminManagerUserHead;
			} else {
				this.telNum = MainConstant.OrgManagerUserHead + BaseDataManager.getInstance().getLoginNode().getLogic_NodeNumber();
			}
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public boolean isUserRole_System() {
		return "admin".equals(name);
	}

	public boolean isUserRole_OrgAdmin() {
		if (isUserRole_System()) {
			return true;
		}

		if (telNum != null)
			return telNum.startsWith(MainConstant.OrgManagerUserHead);
		return false;
	}

	public void setUserTelphone(String telNum) {
		this.telNum = telNum;
		initDcsUser();
	}

	public String getUserTelphone() {
		// TODO
		return this.telNum;
	}

	public MyUser getUser() {
		if (user == null) {
			user = BaseDataManager.getInstance().userGet(this.telNum);
		}
		return user;
	}

	public void setUser(MyUser user) {
		System.out.println("设置用户!!!!!");
		this.user = user;
	}

	public void clear() {
		user = null;
		instance = null;
	}

	private  String serverPort ;
	private  String serverIp ;

	public  String getServerPort() {
		return serverPort;
	}

	public  void setServerPort(String serverPort) {
		this.serverPort = serverPort;
	}

	public String getServerIp() {
		return serverIp;
	}

	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}

	/**
	 * 设置服务器版本
	 * @param serverType
	 */
	public void setServerVerType(int serverType){
		SettingDataManager.getInstance().setIniValue(SettingConstant.SYS.NAME, SettingConstant.SYS.ServerVerType, serverType+"");
	}

	/**
	 * 设置当前数据使用情况
	 * @param type
	 */
	public void setSysType(int type){
		SettingDataManager.getInstance().setIniValue(SettingConstant.SYS.NAME, SettingConstant.SYS.type, type+"");
	}
}
